package com.lagou.framework.mvc;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * @author srh
 * @date 2020/03/14
 **/
public class MethodHandler {

    /**
     * method.invoke(instance, args)
     */
    private Object instance;
    /**
     * executor method
     */
    private Method method;
    /**
     * matches uri
     */
    private Pattern pattern;
    /**
     * 参数映射(HttpServletRequest : 1) = 参数名:参数角标
     */
    private Map<String, Integer> parameterMappings;

    public MethodHandler(Object instance, Method method, Pattern pattern) {
        this.instance = instance;
        this.method = method;
        this.pattern = pattern;
        this.parameterMappings = new HashMap<>(16);
    }

    public Object getInstance() {
        return instance;
    }

    public void setInstance(Object instance) {
        this.instance = instance;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public Pattern getPattern() {
        return pattern;
    }

    public void setPattern(Pattern pattern) {
        this.pattern = pattern;
    }

    public Map<String, Integer> getParameterMappings() {
        return parameterMappings;
    }

    public void setParameterMappings(Map<String, Integer> parameterMappings) {
        this.parameterMappings = parameterMappings;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof MethodHandler)) {
            return false;
        }
        MethodHandler that = (MethodHandler) o;
        return getInstance().equals(that.getInstance()) &&
                getMethod().equals(that.getMethod()) &&
                getPattern().equals(that.getPattern()) &&
                getParameterMappings().equals(that.getParameterMappings());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getInstance(), getMethod(), getPattern(), getParameterMappings());
    }
}
